let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,  //页码
    limit: 10,
    scrollTop: 0,  //初始化滚动距离
    list:[],
    type:1,
    projectTotal:0,
    status:0,
    keyword:'',
    score:0
  },

  onTab:function(e){
    this.setData({type:e.currentTarget.dataset.index,list:[]});
    this.getProject();
  },


  toHelpDetail:function(e){

    wx.navigateTo({ url: `/pages/helpDetail/helpDetail?id=${e.currentTarget.dataset.id}&type=${this.data.type}` })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      if(options.type!=undefined){
        this.setData({type:options.type});
      }
      if(options.status!=undefined){
        this.setData({status:options.status});
      }
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({list:[]});
    this.getProject();
  },

  getProject(){

    var page = this.data.page;
    var limit = this.data.limit;
    var type = this.data.type;
    var keyword = this.data.keyword;
    var score = this.data.score;
    var params = {
      appid: app.globalData.appid,
      type:type,
      page:page,
      limit:limit,
      status:this.data.status,
      keyword:keyword,
      score:score
    };

    //获取求助列表
    app.request('post', '/api/Help/helpList', params).then(res => {
      var list = this.data.list;
      var apilist = res.data.list;
      for(var i=0;i<apilist.length;i++){
        list.push(apilist[i]);
    }
      this.setData({list, list,projectTotal: res.data.count});
    }).catch(err => console.log(err));
  },

  onSubmit:function(){
    this.setData({list:[]});
    this.getProject();
  },

  inputSearch:function(e){
      this.setData({keyword:e.detail.value});
  },
  onSearchBy:function(e){
    this.setData({score:e.currentTarget.dataset.by,list:[]});
    this.getProject();
  },



    /** 滚动触底 */
    scrollBottom(e){
      let page = this.data.page;
      
      if(this.data.list.length >= 10){
          if(page * this.data.limit < this.data.projectTotal){
              page += 1;
              this.setData({ page });
              this.getProject();
          }else{
              app.showFailToast('已经到底了');
          }
      }else{
          app.showFailToast('没有更多数据了');
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})